﻿using Reckoning;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;

namespace TestReckoning
{
    
    
    /// <summary>
    ///This is a test class for CounterTest and is intended
    ///to contain all CounterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CounterTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        private bool CompareDictionaries(Dictionary<string, uint> wordCountsExpected, Dictionary<string, uint> wordCounts)
        {
            bool ok = wordCountsExpected.Count == wordCounts.Count;
            if (ok)
            {
                foreach (KeyValuePair<string, uint> pair in wordCounts)
                {
                    if (!wordCountsExpected.ContainsKey(pair.Key) ||
                        wordCountsExpected[pair.Key] != pair.Value)
                        ok = false;
                }
            }

            return ok;
        }

        /// <summary>
        ///A test for Counter Constructor
        ///</summary>
        [TestMethod()]
        public void CounterConstructorTest()
        {
            Counter target = new Counter();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for CountTotal
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void CountTotalTest()
        {
            Counter_Accessor target = new Counter_Accessor(); 
            Dictionary<string, uint> wordCounts = new Dictionary<string,uint>();
            wordCounts.Add("a", 1);
            wordCounts.Add("b", 2);            
            uint expected = 3; 
            uint actual= target.CountTotal(ref wordCounts);            
            Assert.AreEqual(expected, actual);            
        }

        /// <summary>
        ///A test for IsInWordInterior
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void IsInWordInteriorTest0()
        {
            Counter_Accessor target = new Counter_Accessor();
            string input = "hello";
            int index = 0;
            bool expected = false;
            bool actual = target.IsInWordInterior(input, index);
            Assert.AreEqual(expected, actual);            
        }

        /// <summary>
        ///A test for IsInWordInterior
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void IsInWordInteriorTest1()
        {
            Counter_Accessor target = new Counter_Accessor();
            string input = "hello";
            int index = 1;
            bool expected = true;
            bool actual = target.IsInWordInterior(input, index);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsWordPunctuation
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void IsWordPunctuationTest0()
        {
            Counter_Accessor target = new Counter_Accessor(); 
            char c = 'a';             
            bool expected = false; 
            bool actual = target.IsWordPunctuation(ref c);            
            Assert.AreEqual(expected, actual);            
        }

        /// <summary>
        ///A test for IsWordPunctuation
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void IsWordPunctuationTest1()
        {
            Counter_Accessor target = new Counter_Accessor();
            char c = '\'';
            bool expected = true;
            bool actual = target.IsWordPunctuation(ref c);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ProcessBuffer
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void ProcessBufferTest()
        {
            Counter_Accessor target = new Counter_Accessor();
            string buffer = "there are four words are there\n";
            Dictionary<string, uint> wordCounts = new Dictionary<string,uint>();
            target.ProcessBuffer(ref buffer, ref wordCounts);
            Dictionary<string, uint> wordCountsExpected = new Dictionary<string, uint>();
            wordCountsExpected.Add("there", 2);
            wordCountsExpected.Add("are", 2);
            wordCountsExpected.Add("four", 1);
            wordCountsExpected.Add("words", 1);            
            Assert.IsTrue(CompareDictionaries(wordCountsExpected, wordCounts));
        }

        /// <summary>
        ///A test for RemovePunctuation
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void RemovePunctuationTest0()
        {
            Counter_Accessor target = new Counter_Accessor();
            string original = "no; i don't think so!";
            string expected = "no i don't think so ";
            string actual = target.RemovePunctuation(original);
            Assert.AreEqual(expected, actual);            
        }

        /// <summary>
        ///A test for RemovePunctuation
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Reckoning.exe")]
        public void RemovePunctuationTest1()
        {
            Counter_Accessor target = new Counter_Accessor();
            string original = "\"No; I don't think so,\" he said.";
            string expected = "No I don't think so  he said ";
            string actual = target.RemovePunctuation(original);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for RunFile
        ///</summary>
        [TestMethod()]
        public void RunFileTest()
        {
            Counter target = new Counter(); 
            string text = "There are four words are there? no--i don't think so."; 
            string path = "testfile.txt";
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine(text);
            }
            uint totalWordCount = 0;             
            Dictionary<string, uint> wordCounts = new Dictionary<string,uint>();
            uint totalWordCountExpected = 11;
            Dictionary<string, uint> wordCountsExpected = new Dictionary<string, uint>();
            wordCountsExpected.Add("there", 2);
            wordCountsExpected.Add("are", 2);
            wordCountsExpected.Add("four", 1);
            wordCountsExpected.Add("words", 1);
            wordCountsExpected.Add("no", 1);
            wordCountsExpected.Add("i", 1);
            wordCountsExpected.Add("don't", 1);
            wordCountsExpected.Add("think", 1);
            wordCountsExpected.Add("so", 1);            
            target.RunFile(path, out totalWordCount, out wordCounts);
            Assert.AreEqual(totalWordCountExpected, totalWordCount);
            Assert.IsTrue(CompareDictionaries(wordCountsExpected, wordCounts));            
        }

        /// <summary>
        ///A test for RunText
        ///</summary>
        [TestMethod()]
        public void RunTextTest()
        {
            Counter target = new Counter();
            string text = "There are four words are there? no--i don't think so."; 
            uint totalWordCount = 0;
            Dictionary<string, uint> wordCounts = new Dictionary<string, uint>();
            uint totalWordCountExpected = 11;
            Dictionary<string, uint> wordCountsExpected = new Dictionary<string, uint>();
            wordCountsExpected.Add("there", 2);
            wordCountsExpected.Add("are", 2);
            wordCountsExpected.Add("four", 1);
            wordCountsExpected.Add("words", 1);
            wordCountsExpected.Add("no", 1);
            wordCountsExpected.Add("i", 1);
            wordCountsExpected.Add("don't", 1);
            wordCountsExpected.Add("think", 1);
            wordCountsExpected.Add("so", 1);
            target.RunText(text, out totalWordCount, out wordCounts);
            Assert.AreEqual(totalWordCountExpected, totalWordCount);
            Assert.IsTrue(CompareDictionaries(wordCountsExpected, wordCounts));
        }
    }
}
